package com.wyzz.hr.exportfile;

import java.io.IOException;
import java.lang.reflect.Method;
import java.math.BigDecimal;
import java.util.List;

import com.wyzz.hr.base.Constants;
import com.wyzz.hr.pojo.Empsalarypay;
import com.wyzz.hr.util.MyTools;

import jxl.write.*;
import jxl.write.Number;
import jxl.write.biff.RowsExceededException;

public class ExportPayslip extends MutipleSheetExportHandler implements
		Constants {

	public ExportPayslip(List results[], List titles[]) {
		super(results, titles);
		doSort(results);
	}

	protected void buildTitle() throws RowsExceededException, WriteException {
	}

	protected void buildBody() throws RowsExceededException, WriteException,
			IOException {
		int size = results.length;
		int itemsSize[] = new int[size];
		int count = -2;
		String currentDept = null;
		int workSheetIndex = 0;
		for (int i = 0; i < size; i++) {
			List titleList = titles[i];
			List list = results[i];
			itemsSize[i] = titles[i].size() - 9;
			for (int j = 0; j < list.size(); j++) {
				Empsalarypay emp = (Empsalarypay) list.get(j);
				if (!emp.getEspDept().getId().equals(currentDept)) {
					writableSheet = writableWorkbook.createSheet(
							(new StringBuilder()).append("\u5DE5\u8D44\u6761-")
									.append(
											emp.getEspDept()
													.getDepartmentName())
									.toString(), workSheetIndex++);
					writableSheet.setRowView(0, 800);
					currentDept = emp.getEspDept().getId();
					count = 0;
				}
				int sheetTitleLength = titleList.size() - 2;
				writableSheet.addCell(new Label(0, count, "\u90E8\u95E8",
						titleCellFormat));
				writableSheet.addCell(new Label(1, count,
						"\u85AA\u8D44\u5E10\u5957", titleCellFormat));
				for (int k = 0; k < sheetTitleLength; k++)
					writableSheet.addCell(new Label(k + 2, count, titleList
							.get(k + 2).toString(), titleCellFormat));

				count++;
				writableSheet.addCell(new Label(0, count, emp.getEspDept()
						.getDepartmentName(), defaultCellFormat));
				writableSheet.addCell(new Label(1, count, emp.getEspEsavId()
						.getEsavEsac().getEsacName(), defaultCellFormat));
				writableSheet.addCell(new Label(2, count, emp.getEspEmpno()
						.getEmpDistinctNo(), defaultCellFormat));
				writableSheet.addCell(new Label(3, count, emp.getEspEmpno()
						.getEmpName(), defaultCellFormat));
				writableSheet.addCell(new Label(4, count, emp.getEspEmptype()
						.getEmptypeName(), defaultCellFormat));
				writableSheet.addCell(new Label(5, count, emp.getEspLocation()
						.getLocationName(), defaultCellFormat));
				writableSheet.addCell(new Label(6, count, getEmpStaus(
						emp.getEspEmpno().getEmpStatus()).toString(),
						defaultCellFormat));
				writableSheet.addCell(new Label(7, count, MyTools.vigenere(emp
						.getEspEmpconfig().getEscBankAccountNo(),
						MyTools.getUpKey(emp.getEspEmpconfig().getId(),
								MyTools.STRING), MyTools.DECRYPT_MODE),
						defaultCellFormat));
				writableSheet
						.addCell(new Label(8, count, emp.getEspEmpconfig()
								.getEscJobgrade().getJobGradeName(),
								defaultCellFormat));
				int itemSize = itemsSize[i] >= 48 ? 48 : itemsSize[i];
				for (int acssist = 0; acssist < itemSize; acssist++)
					try {
						Class ownerClass = emp.getClass();
						int number = acssist + 1;
						Method getMethod = ownerClass.getMethod(
								(new StringBuilder()).append("getEspColumn")
										.append(number).toString(),
								new Class[0]);
						BigDecimal bd = (BigDecimal) getMethod.invoke(emp,
								new Object[0]);
						Number nb = new Number(9 + acssist, count, bd
								.floatValue(), numberCellFormat);
						writableSheet.addCell(nb);
					} catch (Exception e) {
						e.printStackTrace();
					}

				count += 2;
			}

		}

		writableWorkbook.write();
	}

	private void doSort(List results[]) {
		for (int i = 0; i < results.length; i++) {
			SalaryRecordSorter sorter = new SalaryRecordSorter(results[i]);
			String props[] = { "espEmpno.empDistinctNo" };
			sorter.doSort(props);
		}

	}

	public String getEmpStaus(Integer i) {
		switch (i.intValue()) {
		case 0: // '\0'
			return "\u8F9E\u804C";

		case 1: // '\001'
			return "\u5728\u804C";

		case 2: // '\002'
			return "\u505C\u85AA\u7559\u804C";

		case 3: // '\003'
			return "\u9000\u4F11";

		case 4: // '\004'
			return "\u8F9E\u9000";
		}
		return null;
	}

	private WritableSheet writableSheet;
}
